<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");
$db = new DBC();
$cvs_file = $_FILES ['csv_file'] ['tmp_name'];
if ($cvs_file != "") {
    $row = 0;
    if (($handle = fopen($cvs_file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (count($data) > 0) {
                $code = trim($data[0]);
                if ($code != "" && startWith($code, "M")) {
                    $name = convertEncoding($data[3]);
                    $author = isset($data[4]) ? $data[4] : "";
                    $type = isset($data[7]) ? $data[7] : "";

                    if ($type == "欢乐") {
                        $type = 'a';
                    } else if ($type == "活泼") {
                        $type = 'b';
                    } else if ($type == "抒情") {
                        $type = 'c';
                    } else if ($type == "甜美") {
                        $type = 'd';
                    }

                    $sql = "SELECT code FROM t_music WHERE flashcode = '" . $code . "'";
                    $temp = $db->fetchData($sql);
                    if ($temp != false && count($temp) > 0) {
                        $sql = "UPDATE t_music SET name='" . $name . "',author = '" . $author . "',status = 0,type1='" . $type . "',createdate=now() WHERE flashcode='" . $code . "'";
                    } else {
                        $sql = "INSERT INTO t_music(flashcode,name,author,type1,createdate) VALUES('" . $code . "','" . $name . "','" . $author . "'," . $type . "',now())";
                    }
                    if ($db->execute($sql)) {
                        
                    }
                }
            }
            $row++;
        }
        fclose($handle);
    }
}

echo "<script language='javascript'>location='animationList.php?menu=animation';</script>";

function convertEncoding($name) {
    if (mb_check_encoding($name, "utf-8") != 1) {
        if (mb_check_encoding($name, "gbk") == 1) {
            $name = mb_convert_encoding($name, "utf-8", "gbk");
        } else {
            $name = mb_convert_encoding($name, "utf-8");
        }
    }
    return $name;
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}

function startWith($str, $needle) {
    return strpos($str, $needle) === 0;
}
?>
